﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using WpfApp1.Httpwork;
using MySql.Data.MySqlClient;
using WpfApp1.mysqlunit;
using WpfApp1.bean;
using System.Text.RegularExpressions;

/**登录页面*/
namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window,HttpDengluInterface
    {
        public static String http = "https://erp.knoya.com/";
        public static String path = "";
        public static String token = ""; 
        public static String cookie = "";
        public static String COOKIE = "";
        public static String session_name = "";
        String username = "18820421791";
        String password = "xyy00";
        MysqlData mysqkdata;
        Boolean isdenglu = false;
        public static CookieContainer cookiecon = new CookieContainer();
        public MainWindow()
        {

            InitializeComponent();
            readHttp();

            //UserName.Text = SystemParameters.PrimaryScreenWidth+"ssss"+ SystemParameters.PrimaryScreenHeight;

            //UserName.Text = "18820421791";
            //Password.Password = "xyy00";
            path = AppDomain.CurrentDomain.BaseDirectory;
            Console.WriteLine(path);
            //zhanghu();
            mysqkdata = MysqlData.createMysqlData();
            var bean = mysqkdata.readusersql();
            if (bean.username != null)
            {
                UserName.Text = bean.username;
                Password.Password = bean.password;
            }
            mysqkdata.deletepackage();
            mysqkdata.deletelog();

        }



        public void readHttp()
        {
            string filePath = MainWindow.path + "save\\http_setting.xml";
            if (File.Exists(filePath))
            {
                XElement xmlDoc = XElement.Load(filePath);
                Contacts.contacts.HTTP = xmlDoc.Attribute("http").Value;
                
            }
        }

        //private void zhanghu()
        //{
        //    string filePath = path + "save\\Login.xml";
        //    XElement xmlDoc = XElement.Load(filePath);
        //    token = xmlDoc.Attribute("token").Value;
        //    cookie = xmlDoc.Attribute("cookie").Value;
        //    session_name = xmlDoc.Attribute("session_name").Value;
        //    Console.WriteLine(xmlDoc.Attribute("session_name").Value);
        //    Console.WriteLine(xmlDoc.Attribute("token").Value);
        //    Console.WriteLine(xmlDoc.Attribute("cookie").Value);
        //}

        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            if (isdenglu)
            {
                return;
            }
            //Window1 Mn = new Window1();
            //Mn.Show();
            //Window window = Window.GetWindow(this);//关闭父窗体
            //window.Close();
            login login = new login();
            login.username = UserName.Text;
            login.password = Password.Password;
            string json = JsonConvert.SerializeObject(login);
            Console.WriteLine(json);
            isdenglu = true;
            Http.HttpDenglu(this, json);
            //Post(rul, json);
        }

        //登录成功
        private void JsonResult(string JsonText , HttpWebResponse resp) 
        {
            var json2 = (JObject)JsonConvert.DeserializeObject(JsonText);

            session_name = (String)json2["session_name"]; 
            token = (String)json2["token"];
            //cookiecon.Add(resp.Cookies);
            foreach (Cookie cook in resp.Cookies)
            {
                
                if (cook.Name == session_name)
                {
                    cookie = cook.Value;
                    COOKIE = session_name + "=" + cook.Value;
                }
            }
           
            var user = new User();
            user.username = UserName.Text;
            user.password = Password.Password;
            user.session_name = session_name;
            user.token = token;
            user.cookie = cookie;
            mysqkdata.adduser(user);

            //string filePath = path + "save\\Login.xml";
            //Console.WriteLine(filePath);
            //XElement car = new XElement("Login",
            //                 new XAttribute("session_name", cookie),
            //                 new XAttribute("token", token),
            //                 new XAttribute("cookie", cookie));
            //car.Save(filePath);

            Window1 Mn = new Window1();
            Mn.Show();
            Window window = Window.GetWindow(this);//关闭父窗体
            window.Close();

        }

        public void HttpDengluOk(string data, HttpWebResponse resp)
        {
            isdenglu = false;
            JsonResult(data, resp);
        }

        public void HttpDengluNo(string e, bool b)
        {
            isdenglu = false;
            MessageBox.Show(Unicode2String(e));
        }
        public static string Unicode2String(string source)
        {
            return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(source, x => Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)).ToString());
        }
    }
    class login
    {
        public string username { get; set; }
        public string password { get; set; }
    }

}
